<template>
  <div class="videoUploader-c">
    <van-uploader
      :after-read="afterRead"
      accept="image/*,video/*"
      :disabled="disabled"
      v-if="method === 'album'"
    >
      <img class="btn-icon" src="../images/pic-icon.png" />
    </van-uploader>
    <van-uploader
      v-else
      :after-read="afterRead"
      accept="image/*"
      capture="camera"
      mutiple
      :disabled="disabled"
    >
      <img class="btn-icon" src="../images/camera-icon.png" />
    </van-uploader>
    <div class="videoUploader-preview" v-if="showPreview">
      <div class="videoUploader-content">
        <video
          v-if="isVideo"
          class="videoplayer"
          :src="previewUrl"
          x5-video-player-type="h5"
          x5-video-player-fullscreen="true"
          x5-video-orientation="portrait"
          controls="controls"
          style="object-fit:fill"
        >
          <source :src="previewUrl" type="video/mp4" />
          你的手机暂不支持视频播放
        </video>
        <img v-if="isImage" :src="previewUrl" />
      </div>
      <div class="videoUploader-operations">
        <div class="cancel" @click="cancel">取消</div>
        <div class="send" @click="send">发送</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'uplader-preview',
  data() {
    return {
      showPreview: false,
      isVideo: false,
      isImage: false,
      previewUrl: null,
      fileObj: null
    };
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    method: { type: String, default: 'album' }
  },
  methods: {
    cancel() {
      this.showPreview = false;
      this.isVideo = false;
      this.isImage = false;
      this.previewUrl = null;
      this.fileObj = null;
    },
    afterRead(fileObj) {
      const { file } = fileObj;
      if (!file) return;
      const { name, size } = file;
      const isVideo = /[(mp4)|(mov)]$/gi.test(name);
      const isImage = /[(jpg)|(jpeg)|(png)|(gif)]$/gi.test(name);
      const fileSize = size / 1024 / 1024;

      if (!isVideo && !isImage) {
        this.$toast.fail('仅支持jpg/jpeg/png格式的图片和mp4/mov格式的视频');
        return;
      }

      if (fileSize > 20) {
        this.$toast.fail('文件不能超过20MB');
        return;
      }
      this.isVideo = isVideo;
      this.isImage = isImage;
      this.fileObj = fileObj;
      this.showPreview = true;
      this.previewUrl = window.URL.createObjectURL(fileObj.file);
    },
    send() {
      this.$emit('after-read', this.fileObj);
      this.$nextTick(() => {
        this.cancel();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.videoUploader-c {
  display: flex;
  align-items: center;
  .btn-icon {
    width: 60px;
    height: 60px;
  }
  .videoUploader-preview {
    background-color: #333333;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 48px;
    box-sizing: border-box;
    .videoUploader-title {
      color: #ffffff;
      line-height: 30px;
    }
    .videoUploader-content {
      width: 80%;
      height: 80%;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        max-width: 100%;
        max-height: 100%;
      }
      .videoplayer {
        max-width: 100%;
        max-height: 100%;
      }
    }
    .videoUploader-operations {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      background-color: #666666;
      display: flex;
      justify-content: space-between;
      .cancel,
      .send {
        color: #ffffff;
        margin: 10px 20px;
        padding: 5px 10px;
      }
      .send {
        border-radius: 14px;
        background: rgba(28, 172, 25, 1);
        border-radius: 17px;
      }
    }
  }
}
</style>
